home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Internet / WWW / gform.1.1 / util.c < prev   
C/C++ Source or Header  |  1995-09-24  |  3KB  |  197 lines

  1. #include <stdio.h>
  2. #include <sys/param.h>
  3. #include <string.h>
  4. #include <pwd.h>
  5. #include "config.h"
  6.  
  7.  
  8. #define LF 10
  9. #define CR 13
  10.  
  11. void *malloc();
  12. void *realloc();
  13.  
  14. char *tildeexpand();
  15. void getword();
  16. char *makeword();
  17. char *fmakeword();
  18. char x2c();
  19. void unescape_url();
  20. void plustospace();
  21. int rind();
  22. int getline();
  23. void send_fd();
  24.  
  25. char *tildeexpand(filename)
  26. char *filename; 
  27. {
  28.     char *expanded, *orig, *tmp, save;
  29.     struct passwd *ppass;
  30.  
  31.     filename++;
  32.  
  33.     for (tmp=filename; *tmp != '\0' && *tmp != '/'; tmp++) ;
  34.  
  35.     save = *tmp;
  36.     *tmp = '\0';
  37.  
  38.     ppass = getpwnam(filename);
  39.  
  40.     *tmp = save;
  41.     
  42.     if (ppass == NULL)  
  43.         return orig;
  44.     
  45.     expanded = (char *) malloc(MAXPATHLEN + 1);
  46.  
  47. #ifdef USERDIR
  48.     if (USERDIR && strlen(USERDIR)>0)
  49.         sprintf(expanded, "%s/%s%s", ppass->pw_dir, USERDIR, tmp);
  50.     else
  51.         sprintf(expanded, "%s%s", ppass->pw_dir, tmp);
  52.     return expanded;
  53. #endif
  54. }
  55.             
  56.  
  57.  
  58. void getword(word,line, stop)
  59. char *word, *line, stop; 
  60. {
  61.     int x = 0,y;
  62.  
  63.     for(x=0;((line[x]) && (line[x] != stop));x++)
  64.         word[x] = line[x];
  65.  
  66.     word[x] = '\0';
  67.     if(line[x]) ++x;
  68.     y=0;
  69.  
  70.     while(line[y++] = line[x++]);
  71. }
  72.  
  73. char *makeword(line, stop)
  74. char *line, stop; 
  75. {
  76.     int x = 0,y;
  77.     char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));
  78.  
  79.     for(x=0;((line[x]) && (line[x] != stop));x++)
  80.         word[x] = line[x];
  81.  
  82.     word[x] = '\0';
  83.     if(line[x]) ++x;
  84.     y=0;
  85.  
  86.     while(line[y++] = line[x++]);
  87.     return word;
  88. }
  89.  
  90. char *fmakeword(f, stop, cl)
  91. FILE *f;
  92. char stop;
  93. int *cl; 
  94. {
  95.     int wsize;
  96.     char *word;
  97.     int ll;
  98.  
  99.     wsize = 102400;
  100.     ll=0;
  101.     word = (char *) malloc(sizeof(char) * (wsize + 1));
  102.  
  103.     while(1) {
  104.         word[ll] = (char)fgetc(f);
  105.         if(ll==wsize) {
  106.             word[ll+1] = '\0';
  107.             wsize+=102400;
  108.             word = (char *) realloc(word,sizeof(char)*(wsize+1));
  109.         }
  110.         --(*cl);
  111.         if((word[ll] == stop) || (feof(f)) || (!(*cl))) {
  112.             if(word[ll] != stop) ll++;
  113.             word[ll] = '\0';
  114.             return word;
  115.         }
  116.         ++ll;
  117.     }
  118. }
  119.  
  120. char x2c(what)
  121. char *what;
  122. {
  123.     register char digit;
  124.  
  125.     digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
  126.     digit *= 16;
  127.     digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
  128.     return(digit);
  129. }
  130.  
  131. void unescape_url(url)
  132. char *url;
  133. {
  134.     register int x,y;
  135.  
  136.     for(x=0,y=0;url[y];++x,++y) {
  137.         if((url[x] = url[y]) == '%') {
  138.             url[x] = x2c(&url[y+1]);
  139.             y+=2;
  140.         }
  141.     }
  142.     url[x] = '\0';
  143. }
  144.  
  145. void plustospace(str)
  146. char *str;
  147. {
  148.     register int x;
  149.  
  150.     for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
  151. }
  152.  
  153. int rind(s,c)
  154. char *s, c;
  155. {
  156.     register int x;
  157.     for(x=strlen(s) - 1;x != -1; x--)
  158.         if(s[x] == c) return x;
  159.     return -1;
  160. }
  161.  
  162. int getline(s, n, f)
  163. char *s;
  164. int n;
  165. FILE *f; 
  166. {
  167.  
  168.     register int i=0;
  169.  
  170.     while(1) {
  171.         s[i] = (char)fgetc(f);
  172.  
  173.         if(s[i] == CR)
  174.             s[i] = fgetc(f);
  175.  
  176.         if((s[i] == 0x4) || (s[i] == LF) || (i == (n-1))) {
  177.             s[i] = '\0';
  178.             return (feof(f) ? 1 : 0);
  179.         }
  180.         ++i;
  181.     }
  182. }
  183.  
  184. void send_fd(f, fd)
  185. FILE *f, *fd;
  186. {
  187.     int num_chars=0;
  188.     char c;
  189.  
  190.     while (1) {
  191.         c = fgetc(f);
  192.         if(feof(f))
  193.             return;
  194.         fputc(c,fd);
  195.     }
  196. }
  197.